//
//  SignUpViewController.swift
//  Fancyswap
//
//  Created by Karim Safraoui on 2015-03-14.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

class SignUpViewController : UITableViewController {
    
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var email: UITextField!
    @IBOutlet weak var pwd1: UITextField!
    
    @IBOutlet weak var pwd2: UITextField!
    
    @IBOutlet weak var postal_code: UITextField!
    
    @IBOutlet weak var signup_btn: UIButton!
    
    let t = FancyTools()
    var dismissDelegate: protocol<FancyViewControllerProtocol>?
    
    // for move up view when keyboard appear
    @IBOutlet var textFieldToBottomLayoutGuideConstraint: NSLayoutConstraint!
    
    @IBAction func signup(sender: UIButton) {
        
        
        let username_var    = username.text
        let email_var       = email.text
        let pwd_var1        = pwd1.text
        let pwd_var2        = pwd2.text
        let cp_var          = postal_code.text
        
        if(username_var.isEmpty || email_var.isEmpty || pwd_var1.isEmpty || pwd_var1 != pwd_var2 ){
        
            t.alert_push(  "Attention",msg:"Tous les champs sont requis, les 2 pwd doivent etre identiques")
        }
        else{
            
            t.web_service(  // self.signupAction,
                            {(json_response : JSON) in
                                
                                let auth = Authentification()
                                if( auth.authenticate( json_response , remember:false, un:username_var, pw:pwd_var1 ) == true ){
                                    var storyboard = UIStoryboard(name: "Main", bundle: nil)
                                    var controller = storyboard.instantiateViewControllerWithIdentifier("main") as UIViewController
                                    self.presentViewController(controller, animated: true, completion: nil)
                                }
                            },
                            type_protocol:"POST",
                            rest_fancy_end_point: "register",
                            params: ["username":username_var,"email":email_var,"password":pwd_var1,"password2":pwd_var2,"postalCode":cp_var],
                            encodeJson:true
                    );
        }
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "closeKeyboard")
        self.view.addGestureRecognizer(tap)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name:UIKeyboardWillHideNotification, object: nil);
    }
    
    func keyboardWillShow(sender: NSNotification) {
        
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            self.textFieldToBottomLayoutGuideConstraint?.constant += keyboardSize.height
        }
    }
    
    func keyboardWillHide(sender: NSNotification) {
        
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            self.textFieldToBottomLayoutGuideConstraint?.constant -= keyboardSize.height
        }
    }
    
    @IBAction func home(sender: AnyObject) {
        
        self.dismiss(sender)
    }
    
    func dismiss(sender: AnyObject) {
        
        if self.dismissDelegate != nil {
            self.dismissDelegate!.dismissViewControllerAndTakeAction(self)
            return
        }
        
        self.dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    func closeKeyboard() {
        self.view.endEditing(true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}